package com.zp.resource;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.stereotype.Component;

import com.sun.jersey.spi.resource.Singleton;
import com.zp.bean.common.ResultCommonVo;
import com.zp.bean.order.ResultScheduleTypeVo;
import com.zp.bean.order.ScheduleTypeVo;
import com.zp.domain.ScheduleGroup;
import com.zp.domain.ScheduleType;
import com.zp.resource.common.BaseServices;
import com.zp.util.Constant;
import com.zp.util.MyException;

/**
 * schedule type services
 * 
 * @author chenlijun08@gmail.com
 * 
 * @version 1.0
 * 
 * @date 2014/2/24
 * 
 */
@Component
@Path("/rest/schtype")
@Singleton
public class ScheduleTypeServices extends BaseServices {
	/**
	 * add schtype path:/schtype/addScheduleType Http:POST
	 * 
	 * @param schtype
	 * 
	 * @return Response,ResultCommonVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":1}
	 */
	@POST
	@Path("/addScheduleType")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public Response addScheduleType(ScheduleType schtype) {
		ResultCommonVo result = new ResultCommonVo();
		try {
			this.jpaDao.save(schtype);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * update ScheduleType information path:/schtype/updateScheduleType Http:PUT
	 * 
	 * @param schtype
	 * 
	 * @return Response,ResultScheduleTypeVo with JSON String like:
	 */
	@PUT
	@Path("/updateScheduleType")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public Response updateScheduleType(ScheduleType schtype) {
		ResultScheduleTypeVo result = new ResultScheduleTypeVo();
		try {
			this.jpaDao.update(schtype);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
			ScheduleTypeVo vo = new ScheduleTypeVo();
			vo.setEndTime(schtype.getEndTime());
			vo.setId(schtype.getId());
			vo.setName(schtype.getName());
			vo.setStartTime(schtype.getStartTime());
			vo.setStatus(schtype.getStatus());
			vo.setTypeCode(this.getTypeByCode(schtype.getTypeCode()));
			result.setSt(vo);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * get schedule type by id path:/type/getScheduleType Http:GET
	 * 
	 * @param id
	 *            primary key of ScheduleType
	 * 
	 * @return Response,ResultScheduleTypeVo with JSON String like:
	 */
	@GET
	@Path("/getScheduleType")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getScheduleTypeById(@QueryParam("id") String id) {
		logger.info("getScheduleTypeById called id:" + id);
		ResultScheduleTypeVo result = new ResultScheduleTypeVo();
		try {
			ScheduleType schtype = (ScheduleType) jpaDao.findById(
					ScheduleType.class, id);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
			ScheduleTypeVo vo = new ScheduleTypeVo();
			vo.setEndTime(schtype.getEndTime());
			vo.setId(schtype.getId());
			vo.setName(schtype.getName());
			vo.setStartTime(schtype.getStartTime());
			vo.setStatus(schtype.getStatus());
			vo.setTypeCode(this.getTypeByCode(schtype.getTypeCode()));
			result.setSt(vo);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * delete ScheduleType by id path:/schtype/deleteScheduleType Http:DELETE
	 * 
	 * @param id
	 *            primary key of ScheduleType
	 * 
	 * @return Response,ResultCommonVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":1}
	 */
	@DELETE
	@Path("/deleteScheduleType")
	@Produces(MediaType.APPLICATION_JSON)
	public Response deleteScheduleType(@QueryParam("id") String id) {
		logger.info("deleteScheduleType called id:" + id);
		ResultCommonVo result = new ResultCommonVo();
		try {
			this.jpaDao.deleteById(ScheduleType.class, id);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * search schedule type with filter parameters
	 * path:/schtype/searchScheduleType Http:GET
	 * 
	 * @param nameOrCode the name of schedule type or type code, if ignored the value is null
	 * 
	 * @return Response, ResultScheduleTypeVo with JSON String like:
	 */
	@GET
	@Path("/searchScheduleType")
	@Produces(MediaType.APPLICATION_JSON)
	public Response searchScheduleType(
			@QueryParam("nameOrCode") String nameOrCode) {
		logger.info("searchScheduleType called json nameOrCode:" + nameOrCode);
		String jpql = "from ScheduleType where name=? or typeCode=? order by name asc";
		List<?> list = null;
		ResultScheduleTypeVo result = new ResultScheduleTypeVo();
		List<ScheduleTypeVo> lt = new ArrayList<ScheduleTypeVo>();
		result.setCode(Constant.SUCCESS_CODE);
		result.setMessage(Constant.OK);
		if ("".equals(nameOrCode) || nameOrCode == null) {

			list = this.jpaDao.getResultData(ScheduleGroup.class);
			if (list != null && list.size() > Constant.ZERO) {
				result.setTotalCount(list.size());
			}
		} else {
			Object[] param = { nameOrCode, nameOrCode };
			try {
				list = this.jpaDao.getResultData(Constant.NEGATIVE_ONE,
						Constant.NEGATIVE_ONE, jpql, param);
				if (list != null && list.size() > Constant.ZERO) {
					result.setTotalCount(list.size());
				}

			} catch (MyException e) {
				result.setCode(Constant.FAILE_CODE);
				result.setMessage(e.getMessage());
				e.printStackTrace();
			}
		}
		if (list != null && list.size() > Constant.ZERO) {
			for (Object o : list) {
				ScheduleType schtype = (ScheduleType) o;
				ScheduleTypeVo vo = new ScheduleTypeVo();
				vo.setEndTime(schtype.getEndTime());
				vo.setId(schtype.getId());
				vo.setName(schtype.getName());
				vo.setStartTime(schtype.getStartTime());
				vo.setStatus(schtype.getStatus());
				vo.setTypeCode(this.getTypeByCode(schtype.getTypeCode()));
				lt.add(vo);
			}
			result.setList(lt);

		}
		return Response.ok(result).build();
	}

}
